import {
  PERCENTAGE_LOW,
  PERCENTAGE_MIDDLE,
  PERCENTAGE_HIGH
} from '../../constants/answer-progress-state';
import { GroupAnswerItem } from '../service/core/group-score.d';

/**
 * 查找index对应的区间段
 * @param index 当前循环下标
 * @return {percentage: number} 对应的区间段
 */
const createPercentage = (index: number) => {
  let percentage = 1;
  switch (index) {
    case 0:
      percentage = PERCENTAGE_LOW;
      break;
    case 1:
      percentage = PERCENTAGE_MIDDLE;
      break;
    case 2:
      percentage = PERCENTAGE_HIGH;
      break;
    default:
      percentage = 1;
  }
  return percentage;
};

/**
 * 查找当前conclusionId对应的各分数段列表
 * @param feedbackList 反馈总列表
 * @param conclusionId 当前要查找的conclusionId
 * @return {conclusionFeedbackList: []} 当前conclusion对应的分数段列表
 */
export const createConclusionFeedbackList = (
  feedbackList: any, conclusionId
) => {
  const feedbackListResult = feedbackList.find((feedback: any) => {
    const { conclusionId: itemConclusionId } = feedback;
    return itemConclusionId === conclusionId;
  });
  const { feedbacks: conclusionFeedbackList } = feedbackListResult;
  return conclusionFeedbackList;
};

/**
 * 折合百分比分数
 */
export const convertedScore = (originScoreList: GroupAnswerItem[], feedbackList: []) => {
  const convertedScoreList = originScoreList.map((originScoreItem) => {
    const { conclusionId, conclusionName, totalScore } = originScoreItem;
    const conclusionFeedbackList = createConclusionFeedbackList(
      feedbackList,
      conclusionId
    );
    let cumulative = 0;
    for (let index = 0; index < conclusionFeedbackList.length; index += 1) {
      const { upper, lower } = conclusionFeedbackList[index];
      const percentage = createPercentage(index);
      if (totalScore > upper) {
        cumulative += percentage;
      }
      if (totalScore <= upper && totalScore >= lower) {
        if (totalScore === lower) {
          cumulative += 1;
          break;
        }
        cumulative += ((totalScore - lower) / (upper - lower)) * percentage;
        break;
      }
    }
    return {
      conclusionId,
      conclusionName,
      cumulative: Number(cumulative.toFixed(2)),
    };
  });
  return convertedScoreList;
};

/**
 * 添加二级分类
 * @param classficationList 二级分类总表
 * @param convertedScoreList 已转换百分比的列表
 * @return {echartsDataResult} echarts图表数据
 */
export const classficationConvertedScore = (
  classficationList,
  convertedScoreList
) => {
  const echartsDataResult = classficationList.map((classficationItem) => {
    const {
      classficationId,
      classficationName,
      Conclusions: conclusionsList
    } = classficationItem.dataValues;
    const conclusionsResult = conclusionsList.map((conclusionsItem) => {
      const { id: classficationConclusionId } = conclusionsItem;
      const temp = convertedScoreList.find((conclusion: any) => {
        const { conclusionId } = conclusion;
        return conclusionId === classficationConclusionId;
      });
      return temp;
    });
    return {
      classficationId,
      classficationName,
      conclusionsResult
    };
  });
  return echartsDataResult;
};
